package com.china.api.web.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.china.itapi.common.entity.Users;

import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for Users
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.fdway.po.Users
 * @author MyEclipse Persistence Tools
 */

public class UsersDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UsersDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String PWD = "pwd";
	public static final String BACK1 = "back1";
	public static final String BACK2 = "back2";
	public static final String BACK3 = "back3";
	
	public Session session=getSession();
	public Transaction tc;
	
	public void start(){
	 	tc=session.beginTransaction();
	 	tc.begin();
	 	session.clear();
	}
	
	public void commit(){
		tc.commit();
	}
	public void close(){
		session.close();
	}

	public void save(Users transientInstance) {
		log.debug("saving Users instance");
		try {
			session.save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(Users transientInstance) {
		log.debug("saving Users instance");
		try {
			session.update(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Users persistentInstance) {
		log.debug("deleting Users instance");
		try {
			session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Users findById(java.lang.Long id) {
		log.debug("getting Users instance with id: " + id);
		try {
			session.clear();
			Users instance = (Users) session.get("com.fdway.po.Users", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Users> findByExample(Users instance) {
		log.debug("finding Users instance by example");
		try {
			session.clear();
			List<Users> results = (List<Users>) session.createCriteria(
					"com.fdway.po.Users").add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public Users isLgon(String name, Object pwd) {
		log.debug("finding Users instance with property: " + name
				+ ", value: " + pwd);
		try {
			session.clear();
			String queryString = "from Users as model where model.name= ? and model.pwd= ? ";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, name);
			queryObject.setParameter(1, pwd);
			
			if(queryObject.list().size()>0){
				return (Users) queryObject.list().get(0);
			}else{
				return null;
			}
			
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Users instance with property: " + propertyName
				+ ", value: " + value);
		try {
			session.clear();
			String queryString = "from Users as model where model."
					+ propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Users> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Users> findByPwd(Object pwd) {
		return findByProperty(PWD, pwd);
	}

	public List<Users> findByBack1(Object back1) {
		return findByProperty(BACK1, back1);
	}

	public List<Users> findByBack2(Object back2) {
		return findByProperty(BACK2, back2);
	}

	public List<Users> findByBack3(Object back3) {
		return findByProperty(BACK3, back3);
	}

	public List findAll() {
		log.debug("finding all Users instances");
		try {
			session.clear();
			String queryString = "from Users order by id";
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Users merge(Users detachedInstance) {
		log.debug("merging Users instance");
		try {
			session.clear();
			Users result = (Users) session.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Users instance) {
		log.debug("attaching dirty Users instance");
		try {
			session.clear();
			session.saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Users instance) {
		log.debug("attaching clean Users instance");
		try {
			session.clear();
			session.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}